import message from '@/js/util/message.js';
import register from '@/js/main/register.js';
import system from '@/js/main/system.js';

let event = {
    visit(param, pageList) {
        let event = param.page ? param.page: param;
        for (let i = 0; i < pageList.length; i++) {
            if (event.includes(i)) {
                pageList[i].visible = true;
            } else {
                pageList[i].visible = false;
            }
        }
    },
    alert(param) {
        if (param) {
            setTimeout(() => {
                message.message(param, 'success');
            }, 0);
        }
    },
    view(param) {
        if (register.getComponentById(system.SYSTEM_PREVIEW_ELEMENT + param.elementID)) {
            register.getComponentById(system.SYSTEM_PREVIEW_ELEMENT + param.elementID).showAirCityExplorer(param);
        }
    },
    async interactive(param) {
        switch (param.activeInterac) {
            case 'highligh':
                if (param.visible) {
                    await window.__g.tileLayer.highlightActor(param.Id, param.ObjectID || '');
                } else {
                    await window.__g.tileLayer.stopHighlightActor(param.Id, param.ObjectID || '');
                }
                break;
            case 'visit':
                if (param.visible) {
                    if (param.Id && param.ObjectID) {
                        await window.__g.tileLayer.showActor(param.Id, param.ObjectID);
                    } else {
                        await window.__g.infoTree.show(param.Id);
                    }
                } else {
                    if (param.Id && param.ObjectID) {
                        await window.__g.tileLayer.hideActor(param.Id, param.ObjectID);
                    } else {
                        await window.__g.infoTree.hide(param.Id);
                    }
                }
                break;
            case 'focus':
                if (param.Type && param.Type === 'marker3d') {
                    await window.__g.infoTree.focus(param.Id);
                } else {
                    await window.__g.tileLayer.focusActor(param.Id, param.ObjectID);
                }
                break;
            case 'camera':
                await window.__g.camera.set(+param.x, +param.y, +param.z, +param.pitch, +param.yaw, +param.flyTime);
                break;
            case 'weather':
                if (param.type === 'disable') {
                    await window.__g.weather.disableRainSnow();
                    await window.__g.weather.setSunIntensity(param.rainsnowStrength);
                } else if (param.type === 'rain') {
                    await window.__g.weather.setCloudThickness(2);
                    await window.__g.weather.setRainParam(param.rainsnowStrength, 1, 1);
                } else if (param.type === 'snow') {
                    await window.__g.weather.setCloudThickness(2);
                    await window.__g.weather.setSnowParam(param.rainsnowStrength, 1, 1);
                }
                break;
            case 'weatherTime':
                let startTime = [+param.start.split('-')[0], +param.start.split('-')[1]];
                let endTime = [+param.end.split('-')[0], +param.end.split('-')[1]];
                await window.__g.weather.simulateTime(startTime,endTime,+param.duration);
                break;
            case 'flicker':
                if (param.visible) {
                    // await window.__g.tileLayer.hideActor(param.Id, param.ObjectID);
                    await window.__g.customObject.clear();
                    await window.__g.customObject.addByTileLayer({
                        id: param.growId,
                        tileLayerId: param.Id,
                        //注意5.3新增特性：数组参数 也支持复制单个构件
                        objectId: param.ObjectID,
                        coordinateType: 0,// 坐标系类型 
                        scale: [1, 1, 1],//模型缩放
                        smoothMotion: 0 //1: 平滑移动，0: 跳跃移动
                    });
                    await window.__g.customObject.glow([{ id: param.growId, color: [1, 0, 0, 1], duration: +param.strength, interval: +param.frequency }]);
                } else {
                    await window.__g.customObject.stopGlow([param.growId]);
                    await window.__g.customObject.delete(param.growId);
                    // await window.__g.tileLayer.showActor(param.Id, param.ObjectID);
                }
                break;
            default:
                break;
        }
    },
    async viewExplorer(param) {
        await window.__g.camera.stopAnimation();
        await window.__g.camera.playAnimation(param.id);
    },
    async block(param) {
        try {
            // let val = new Function(param);
            // let eleResult = val(); // 这里必须调用val(),不然不会执行
            // if (!eleResult) {
            //     eleResult = eval(param);
            // }
            // val = null;
            try {
                eval('(async ()=>{' + param + '})()');
            } catch (e) {
                console.log(e);
            }
        } catch (err) {
            console.info('错误');
        }
    },
    eventList: [
        {
            key: 'visit',
            panel: 'VisitEvent',
        },
        {
            key: 'alert',
            panel: 'AlertEvent',
        },
        {
            key: 'view',
            panel: 'ViewEvent',
        },
        {
            key: 'viewExplorer', // 嵌入explorer时候才有
            panel: 'viewExplorerEvent',
        },
        {
            key: 'interactive',
            panel: 'InteractiveEvent',
        },
        {
            key: 'block',
            panel: 'BlockEvent',
        }
    ],
    eventTypeList: {
        "common": [
            {
                label: '鼠标点击时', action: 'click'
            }, {
                label: '鼠标移入时', action: 'mouseenter'
            }, {
                label: '鼠标移出时', action: 'mouseleave'
            }, {
                label: '初始化激活', action: 'initEvent'
            }
        ],
        "VSwitch": [
            {
                label: '开关开启', action: 'on'
            }, {
                label: '开关关闭', action: 'off'
            },
        ],
        "Tree": [
            {
                label: '勾选', action: 'check'
            }, {
                label: '取消勾选', action: 'uncheck'
            }, {
                label: '节点选中', action: 'select'
            }
        ],
        "Timing": [
            {
                label: '定时事件', action: 'timer'
            }
        ],
        "Menu": [
            {
                label: '节点选中', action: 'select'
            }
        ],
        "TimerGroupOption": [
            {
                label: 1,
                value: 1
            },
            {
                label: 2,
                value: 2
            },
            {
                label: 3,
                value: 3
            },
            {
                label: 4,
                value: 4
            },
            {
                label: 5,
                value: 5
            },
            {
                label: 6,
                value: 6
            },
            {
                label: 7,
                value: 7
            },
            {
                label: 8,
                value: 8
            },
            {
                label: 9,
                value: 9
            },
            {
                label: 10,
                value: 10
            }
        ]
    },
    getEventName(name) {
        var label = ''
        switch (name) {
            case 'click':
                label = '鼠标点击';
                break;
            case 'mouseenter':
                label = '鼠标进入';
                break;
            case 'mouseleave':
                label = '鼠标移出';
                break;
            case 'initEvent':
                label = '初始化激活';
                break;
            case 'view':
                label = '导览';
                break;
            case 'alert':
                label = '消息';
                break;
            case 'visit':
                label = '显隐';
                break;
            case 'interactive':
                label = '交互';
                break;
            case 'block':
                label = '代码块';
                break;
            case 'viewExplorer':
                label = '导览';
                break;
            case 'on':
                label = '开关开启';
                break;
            case 'off':
                label = '开关关闭';
                break;
            case 'check':
                label = '勾选';
                break;
            case 'uncheck':
                label = '取消勾选';
                break;
            case 'select':
                label = '节点选中';
                break;
            case 'treeSelect':
                label = '菜单节点选中';
                break;
            case 'timer':
                label = '定时事件';
                break;
            default:
                break;
        }
        return label
    },
    explorerList: [
        {
            label: '显示/隐藏',
            value: 'visit'
        },
        {
            label: '高亮',
            value: 'highligh'
        },
        {
            label: '定位',
            value: 'focus'
        },
        {
            label: '相机',
            value: 'camera'
        },
        {
            label: '天气切换',
            value: 'weather'
        },
        {
            label: '昼夜切换',
            value: 'weatherTime'
        },
        {
            label: '闪烁',
            value: 'flicker'
        }
    ]
};
event.eventMap = {};
for (let i = 0; i < event.eventList.length; i ++) {
    event.eventMap[event.eventList[i].key] = event.eventList[i].panel;
}
export default event;